<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/28
 * Time: 20:09
 */
namespace Services;

use Helpers\Tool;
use Helpers\Main;
use Helpers\HttpClient;
use Helpers\HttpResponse;
use Helpers\User;
use Helpers\XhException;
use Modules\School;
class BigData
{

    /**
     * @var \Helpers\HttpClient
     */
    public static $httpClient;
    public static $url;


    /**
     * 初始化配置
     *
     * @throws XhException
     *
     * create by dale 20180928
     */
    private static function init()
    {
        if (!self::$httpClient) {
            $config     = Main::getConfigByName('bigData');
            self::$url  = Tool::getValue($config, 'rankUrl');
            if (!self::$url) {
                throw new XhException('大数据rankUrl获取失败');
            }
            self::$httpClient = new HttpClient(self::$url);
        }
    }


    /**
     * 获取活跃度
     *
     * @param $schoolId
     * @param $month
     * @param int $dimGroupId
     * @param array $param
     *
     * @return array
     * @throws XhException
     *
     * create by dale  20180928
     */
    public static function getActive($schoolId, $month, $dimGroupId = 1, $param = [])
    {
        self::init();
        $response = self::$httpClient->get('/api/v1/pri/bigdata/manage/active/user/mtd', array_merge([
            'schoolId' => $schoolId,
            'month'    => date('Ym', strtotime($month)),
//            'dimGroupId' => $dimGroupId
        ], $param));
        if (!$response->isSuccess()) {
            throw new XhException('调取活跃度接口失败', $response);
        }
        return $response->toArray();
    }


    /**
     * 获取全校云教辅
     *
     * @param $startTime
     * @param $endTime
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by dale  20181010
     */
    public static function getAllSchoolUseRate($startTime, $endTime)
    {
        self::init();
        $response = self::$httpClient->get('/api/v1/pri/bigdata/manage/cloudwork/use/allSchool', [
            'start' => $startTime,
            'end'   => $endTime
        ]);
        if (!$response->isSuccess()) {
            throw new XhException('暂无数据', json_encode($response, JSON_UNESCAPED_UNICODE));
        }
        return $response->toArray();
    }

    /**
     * 获取各校云教辅
     *
     * @param $schoolIds
     * @param $startTime
     * @param $endTime
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by dale  20181010
     */
    public static function getSchoolUseRate($schoolIds, $startTime, $endTime)
    {
        self::init();
        $params = [
            'start' => $startTime,
            'end'   => $endTime
        ];
        if ($schoolIds) {
            $params['schoolIds'] = $schoolIds;
        }
        $response = self::$httpClient->get('/api/v1/pri/bigdata/manage/cloudwork/use/school', $params);
        if (!$response->isSuccess()) {
            throw new XhException('暂无数据', json_encode($response, JSON_UNESCAPED_UNICODE));
        }
        return $response->toArray();
    }

    /**
     * 获取某校教师云教辅
     *
     * @param $schoolId
     * @param $teacherIds
     * @param $startTime
     * @param $endTime
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by dale  20181010
     */
    public static function getTchUseRate($schoolId, $teacherIds, $startTime, $endTime)
    {
        self::init();
        $params = [
            'schoolId' => $schoolId,
            'start' => $startTime,
            'end'   => $endTime
        ];
        if ($teacherIds) {
            $params['teacherIds'] = $teacherIds;
        }
        $response = self::$httpClient->get('/api/v1/pri/bigdata/manage/cloudwork/use/teacher', $params);
        if (!$response->isSuccess()) {
            throw new XhException('暂无数据', json_encode($response, JSON_UNESCAPED_UNICODE));
        }
        return $response->toArray();
    }

    /**
     * 获取学海题舟考核数据
     *
     * @param $subjectIds
     * @param $schoolId
     * @param $startTime
     * @param $endTime
     * @return mixed
     * @throws \Exception
     *
     * create by ck 20181018
     * modify by ck 20181030    科目可多个
     */
    public static function getSubjectKpiInfo($subjectIds, $schoolId, $startTime, $endTime)
    {
        self::init();
        $response = self::$httpClient->get(
            '/api/v1/pri/bigdata/product_stw/getKpiInfo',
            [
                'subjectIds' => $subjectIds,
                'schoolId'  => $schoolId,
                'startTime' => $startTime,
                'endTime'   => $endTime
            ]
        );
        if (!$response->isSuccess()) {
            throw new \Exception('请求失败：' . json_encode($response, JSON_UNESCAPED_UNICODE));
        }
        $data = $response->toArray();
        return $data;
    }

    /**
     * 获取数学/科学分层统计数据
     *
     * @param $schoolId
     * @param $classId
     * @param $subjectName
     * @param $startTime
     * @param $endTime
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by ck 20181022
     */
    public static function getMathScienceBrushGroup($schoolId, $classId, $subjectName, $startTime, $endTime)
    {
        self::init();
        $response = self::$httpClient->get('/api/v1/pri/bigdata/qstCount/matsci', [
            'schoolid'      => $schoolId,
            'classid'       => $classId,
            'subjectname'   => $subjectName,
            'starttime'     => $startTime,
            'endedtime'     => $endTime
        ]);
        if (!$response->isSuccess()) {
            throw new XhException('请求失败', $response);
        }
        return $response->toArray();
    }

    /**
     * 获取英语分层统计数据
     *
     * @param $schoolId
     * @param $classId
     * @param $startTime
     * @param $endTime
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by ck 20181022
     */
    public static function getEnglishBrushGroup($schoolId, $classId, $startTime, $endTime)
    {
        self::init();
        $response = self::$httpClient->get('/api/v1/pri/bigdata/product_stw/enstwInfo/getEnStwInfo', [
            'schoolId'  => $schoolId,
            'classId'   => $classId,
            'startTime' => $startTime,
            'endTime'   => $endTime
        ]);
        if (!$response->isSuccess()) {
            throw new XhException('请求失败', $response);
        }
        return $response->toArray();
    }

    /**
     * 根据科目获取书本信息
     *
     * @param $subjectId
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by dale  20181022
     */
    public static function getBookBySubject($subjectId)
    {
        self::init();
        $params = [];
        if ($subjectId) {
            $params['subjectId'] = $subjectId;
        }
        $response = self::$httpClient->get('/api/v1/pri/bigdata/book/subject', $params);
        if (!$response->isSuccess()) {
            throw new XhException('请求失败', $response);
        }
        return $response->toArray();
    }

    /**
     * 获取刷题王学生统计数据
     *
     * @param $schoolId
     * @param $starttime
     * @param $endedtime
     * @param $classid
     * @param $bookid
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by dale  20181022
     */
    public static function getStuBrush($schoolId, $starttime, $endedtime, $classid, $bookid)
    {
        self::init();
        $response = self::$httpClient->postJson('/api/v1/pri/bigdata/qstCount/liveHp/totalIntegral', [
            'schoolid'  => $schoolId,
            'starttime'   => $starttime,
            'endedtime' => $endedtime,
            'classid'   => $classid,
            'bookid'   => $bookid
        ]);
        if (!$response->isSuccess()) {
            throw new XhException('请求失败', $response);
        }
        return $response->toArray();
    }

    /**
     * 获取各个应用教师统计数据
     *
     * @param $schoolId
     * @param $tchIds
     * @param $startTime
     * @param $endTime
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by qgz 20181130
     */
    public static function getTeaAppStatData($schoolId, $tchIds, $startTime, $endTime)
    {
        self::init();
        $response = self::$httpClient->post('/api/v1/pri/bigdata/manage/app/teacher/statistics', [
            'schoolId'  => $schoolId,
            'tchIds'   => $tchIds,
            'startTime' => $startTime,
            'endTime'   => $endTime
        ]);
        if (!$response->isSuccess()) {
            throw new XhException($response);
        }
        return $response->toArray();
    }

    /**
     * 获取云听说运营考核统计数据
     *
     * @param $schoolId
     * @param $startTime
     * @param $endTime
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by sqb 20190102
     */
    public static function getHearKpiInfo($schoolId, $startTime = null, $endTime = null)
    {
        self::init();
        $response = self::$httpClient->get('/api/v1/pri/bigdata/xhhear/getHearKpiInfo', [
            'schoolId'  => $schoolId,
            'startTime' => $startTime,
            'endTime'   => $endTime
        ]);
        if (!$response->isSuccess()) {
            throw new XhException($response);
        }
        return $response->toArray();
    }
}
